//标准库
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

// FreeRtos
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "freertos/event_groups.h"
#include "freertos/semphr.h"
#include "esp_freertos_hooks.h"

//系统，无线，事件，日志
#include "esp_system.h"
#include "esp_wifi.h"
#include "esp_bt.h"
#include "esp_event.h"
#include "esp_log.h"

// Light Weight (轻型)IP协议
#include "lwip/err.h"
#include "lwip/sys.h"
#include "lwip/sockets.h"
#include "lwip/netdb.h"
#include "lwip/dns.h"

#include "nvs_flash.h"
#include "esp_attr.h"
#include "esp_tls.h"
#include "cJSON.h"

#include "esp_lcd_ili9341.h"
#include "esp_lcd_panel_io.h"
#include "esp_lcd_panel_vendor.h"
#include "esp_lcd_panel_ops.h"

#include "driver/gpio.h"

#include "sdkconfig.h"

#include "lvgl.h"
#include "lvgl_helpers.h"

#include "startlvgl.h"
#include "gpio.h"
#include "wifi.h"
#include "blufi.h"
#include "DHT11.h"
#include "ntp.h"
#include "https.h"
#include "https_tianqi.h"

nvs_handle_t mnvs_handle;

void memory_check_task(void *pvParameter)

{
    // lv_obj_t *label1 = lv_label_create(lv_scr_act(), NULL);

    // static lv_style_t font_style;
    // lv_style_init(&font_style);
    // lv_style_set_text_font(&font_style, LV_STATE_DEFAULT, &myFont);
    // lv_color_t col = lv_color_make(255, 0, 0);

    // lv_style_set_text_color(&font_style, LV_STATE_DEFAULT, col);
    // lv_obj_add_style(label1, LV_LABEL_PART_MAIN, &font_style);
    // lv_obj_align(label1, NULL, LV_ALIGN_IN_TOP_LEFT, 0, 0);
    // // lv_obj_set_drag(label1,true);

    while (1)
    {
        char show_data[64];
        sprintf(show_data, "I:%d.%dkb\nE:%d.%dkb\nD:%d.%dkb",
                heap_caps_get_free_size(MALLOC_CAP_INTERNAL) / 1024,
                heap_caps_get_free_size(MALLOC_CAP_INTERNAL) % 1024,
                heap_caps_get_free_size(MALLOC_CAP_SPIRAM) / 1024,
                heap_caps_get_free_size(MALLOC_CAP_SPIRAM) % 1024,
                heap_caps_get_free_size(MALLOC_CAP_DMA) / 1024,
                heap_caps_get_free_size(MALLOC_CAP_DMA) % 1024);

        // lv_label_set_text(label1, show_data);
        ESP_LOGW("MEM_INFO","\n%s",show_data);

        vTaskDelay(1000/ portTICK_PERIOD_MS);
    }
    vTaskDelete(NULL);
}

static void ta_event_cb(lv_obj_t *ta, lv_event_t event);

static lv_obj_t *kb;

static void kb_event_cb(lv_obj_t *keyboard, lv_event_t e)
{
    lv_keyboard_def_event_cb(kb, e);
    if (e == LV_EVENT_CANCEL)
    {
        lv_keyboard_set_textarea(kb, NULL);
        lv_obj_del(kb);
        kb = NULL;
    }
}

static void kb_create(lv_obj_t *ta)
{
    /* Create a keyboard */
    kb = lv_keyboard_create(lv_scr_act(), NULL);
    lv_obj_set_size(kb, LV_HOR_RES, LV_VER_RES / 2);

    lv_keyboard_set_textarea(kb, ta);
    lv_keyboard_set_cursor_manage(kb, true); /* Automatically show/hide cursors on text areas */
    lv_obj_set_event_cb(kb, kb_event_cb);
}

void lv_ex_textarea_2(void)
{
    /* Create the password box */
    lv_obj_t *pwd_ta = lv_textarea_create(lv_scr_act(), NULL);
    lv_textarea_set_text(pwd_ta, "");
    lv_textarea_set_pwd_mode(pwd_ta, true);
    lv_textarea_set_one_line(pwd_ta, true);
    lv_textarea_set_cursor_hidden(pwd_ta, true);
    lv_obj_set_width(pwd_ta, LV_HOR_RES / 2 - 20);
    lv_obj_set_pos(pwd_ta, 5, 20);
    lv_obj_set_event_cb(pwd_ta, ta_event_cb);

    /* Create a label and position it above the text box */
    lv_obj_t *pwd_label = lv_label_create(lv_scr_act(), NULL);
    lv_label_set_text(pwd_label, "Password:");
    lv_obj_align(pwd_label, pwd_ta, LV_ALIGN_OUT_TOP_LEFT, 0, 0);

    /* Create the one-line mode text area */
    lv_obj_t *oneline_ta = lv_textarea_create(lv_scr_act(), pwd_ta);
    lv_textarea_set_pwd_mode(oneline_ta, false);
    lv_textarea_set_cursor_hidden(oneline_ta, true);
    lv_obj_align(oneline_ta, NULL, LV_ALIGN_IN_TOP_RIGHT, -5, 20);

    /* Create a label and position it above the text box */
    lv_obj_t *oneline_label = lv_label_create(lv_scr_act(), NULL);
    lv_label_set_text(oneline_label, "Text:");
    lv_obj_align(oneline_label, oneline_ta, LV_ALIGN_OUT_TOP_LEFT, 0, 0);

    kb_create(NULL);
}

static void ta_event_cb(lv_obj_t *ta, lv_event_t event)
{
    if (event == LV_EVENT_CLICKED)
    {
        /* Focus on the clicked text area */
        if (kb != NULL)
        {
            lv_keyboard_set_textarea(kb, ta);
        }
        else
        {
            kb_create(ta);
        }
    }

    else if (event == LV_EVENT_INSERT)
    {
        const char *str = lv_event_get_data();
        if (str[0] == '\n')
        {
            printf("Ready\n");
        }
    }
}

void http_get_taska(){
    for(;;){
        https_get_tianqi();
        vTaskDelay(pdMS_TO_TICKS(2000));
    }

}

void app_main(void)
{
    
    xTaskCreate(&memory_check_task, "memory_cheak_task", 1024*4, NULL, 3, NULL);
    // init_lvgl();
    cur_blec_inf.blufi_is_init = false;
    esp_err_t ret;
    ret = nvs_flash_init();
    if (ret == ESP_ERR_NVS_NO_FREE_PAGES || ret == ESP_ERR_NVS_NEW_VERSION_FOUND)
    {
        ESP_ERROR_CHECK(nvs_flash_erase());
        ret = nvs_flash_init();
    }
    ret = nvs_open("storage", NVS_READWRITE, &mnvs_handle);
    ESP_ERROR_CHECK(ret);

    init_wifi();
    init_gpio();

    // lvgl_KaiJi_create();
    xEventGroupWaitBits(wifi_event_group,
                        WIFI_CONNECTED_BIT,
                        pdFALSE,
                        pdFALSE,
                        1500 / portTICK_PERIOD_MS);

    if (cur_wifi_info.isConnected)
    {
        ESP_LOGI(TAG_WIFI, "WIFI连接成功,不再加载blufi");
    }
    else
    {
        ESP_LOGI(TAG_WIFI, "WIFI连接失败,加载blufi");
        init_blufi();
    }

    // lv_ex_textarea_2();
    
    while(!cur_wifi_info.isConnected){  vTaskDelay(pdMS_TO_TICKS(500));}
    // xTaskCreate(&http_get_taska, "http_get_taska", 1024 *10, NULL, 3, NULL);

    

    // https_get_tianqi();
    // vTaskDelay(3000 / portTICK_PERIOD_MS);
    // esp_lcd_panel_invert_color(panel_handle,1);
    // vTaskDelay(3000 / portTICK_PERIOD_MS);
    // esp_lcd_panel_invert_color(panel_handle,0);
    // vTaskDelay(3000 / portTICK_PERIOD_MS);
    get_now_time();
}
